package com.limeng.server.filter;

import java.io.File;
import java.io.FilenameFilter;
import java.util.List;
import java.util.Map;

public class WebFileFilter implements FilenameFilter {

    private static String FILENAME_WEB_INF = "WEB-INF";

    private static String FILENAME_WEB_CONFIG = "web.xml";

    private static String FILENAME_CLASSES = "classes";

    private static String CLASS_SUFFIX = ".class";

    private String findFileName;

    public WebFileFilter(String findFileName) {
        this.findFileName = findFileName;
    }


    public Map<String,File> listClasses(Map<String,File> files, File sourceFile){
        if(sourceFile.isDirectory()){
            File[] resources = sourceFile.listFiles();
            for (File file: resources) {
                if(file.isDirectory()){
                    return listClasses(files, file);
                }else {
                    String className = file.getName().substring(0,file.getName().indexOf(".class"));
                    files.put(className,file);
                }
            }
        }else{
            if(sourceFile.getName().endsWith(CLASS_SUFFIX)){
                files.put(sourceFile.getName(),sourceFile);
            }
        }
        return files;
    }

    @Override
    public boolean accept(File file, String name) {
        if(name.equals(FILENAME_WEB_INF)){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String classFullName = "Abc.class";
        String className = classFullName.substring(0,classFullName.indexOf(".class"));
        System.out.println(className);
    }
}
